package com.my.mvvmrapid.ui.fuction.dialog;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.webkit.WebView;
import android.widget.Toast;

import com.afollestad.materialdialogs.LayoutMode;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.bottomsheets.BottomSheet;
import com.afollestad.materialdialogs.checkbox.DialogCheckboxExtKt;
import com.afollestad.materialdialogs.color.ColorPalette;
import com.afollestad.materialdialogs.color.DialogColorChooserExtKt;
import com.afollestad.materialdialogs.customview.DialogCustomViewExtKt;
import com.afollestad.materialdialogs.files.DialogFileChooserExtKt;
import com.afollestad.materialdialogs.input.DialogInputExtKt;
import com.afollestad.materialdialogs.list.DialogListExtKt;
import com.afollestad.materialdialogs.list.DialogMultiChoiceExtKt;
import com.afollestad.materialdialogs.list.DialogSingleChoiceExtKt;
import com.my.mvvmrapid.BR;
import com.my.mvvmrapid.R;
import com.my.mvvmrapid.databinding.ActivityDialogBinding;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.zhongjh.mvvmrapid.base.ui.BaseActivity;
import com.zhongjh.mvvmrapid.utils.PermissionsUtil;
import com.zhongjh.mvvmrapid.utils.ToastUtils;
import com.zhongjh.mvvmrapid.utils.ScreenUtil;

import java.util.Arrays;

import io.reactivex.disposables.Disposable;

/**
 * @author zhongjh
 * @date 2021/4/15
 */
public class DialogActivity extends BaseActivity<ActivityDialogBinding, DialogModel> {

    public static Disposable startActivity(RxAppCompatActivity activity) {
        // 判断权限
        final RxPermissions rxPermissions = new RxPermissions(activity);

        return rxPermissions
                .requestEachCombined(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(permission -> PermissionsUtil.managePermissions(permission, activity, new PermissionsUtil.OnPermissions() {
                    @Override
                    public void onGranted() {
                        Intent intent = new Intent(activity, DialogActivity.class);
                        activity.startActivity(intent);
                    }
                }));

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ScreenUtil.setFullScreen(DialogActivity.this, false);
        super.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_dialog;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public boolean isStartAutoHideSoftKeyboard() {
        return false;
    }

    @Override
    public void initParam() {

    }

    @Override
    public void initData() {
        initToolbar(binding.toolbar, "窗口", true);
    }

    @SuppressLint("CheckResult")
    @Override
    public void initViewObservable() {
        binding.btnSimple.setOnClickListener(v -> {
            MaterialDialog dialog = new MaterialDialog(DialogActivity.this, MaterialDialog.getDEFAULT_BEHAVIOR());
            dialog.setTitle("标题");
            dialog.message(-1, "消息", null);
            dialog.positiveButton(-1, "确定", materialDialog -> {
                ToastUtils.showShort("确定");
                return null;
            });
            dialog.negativeButton(-1, "取消", materialDialog -> {
                ToastUtils.showShort("取消");
                return null;
            });
            DialogCheckboxExtKt.checkBoxPrompt(dialog, -1, "选择", false, isChecked -> {
                ToastUtils.showShort("checked? " + isChecked);
                return null;
            });
            dialog.getWindow().getAttributes().windowAnimations = R.style.DialogStyle;
            dialog.show();
        });

        binding.btnList.setOnClickListener(v -> {
            MaterialDialog dialog = new MaterialDialog(DialogActivity.this, MaterialDialog.getDEFAULT_BEHAVIOR());
            dialog.setTitle("标题");
            dialog.message(-1, "消息", null);
            String[] datas = getResources().getStringArray(R.array.week);
            DialogListExtKt.listItems(dialog, -1, Arrays.asList(datas), null,
                    true, (materialDialog, index, text) -> {
                        ToastUtils.showShort("Selected item  " + text + " at index " + index);
                        return null;
                    });
            dialog.positiveButton(-1, "确定", materialDialog -> {
                ToastUtils.showShort("确定");
                return null;
            });
            dialog.negativeButton(-1, "取消", materialDialog -> {
                ToastUtils.showShort("取消");
                return null;
            });
            dialog.show();
        });

        binding.btnRadioButton.setOnClickListener(view -> {
            int[] disabledIndices = {1, 3};

            MaterialDialog dialog = new MaterialDialog(DialogActivity.this, MaterialDialog.getDEFAULT_BEHAVIOR());
            dialog.setTitle("标题");
            DialogSingleChoiceExtKt.listItemsSingleChoice(dialog, R.array.week, null, disabledIndices, 0,
                    true, -1, -1, (materialDialog, index, text) -> {
                        ToastUtils.showShort("Selected item  " + text + " at index " + index);
                        return null;
                    });
            dialog.positiveButton(-1, "确定", materialDialog -> {
                ToastUtils.showShort("确定");
                return null;
            });
            dialog.show();
        });

        binding.btnCheckBox.setOnClickListener(view -> {
            int[] initialSelection = {2, 3};
            int[] disabledIndices = {1, 3};

            MaterialDialog dialog = new MaterialDialog(DialogActivity.this, MaterialDialog.getDEFAULT_BEHAVIOR());
            dialog.setTitle("标题");
            DialogMultiChoiceExtKt.listItemsMultiChoice(dialog, R.array.week, null, disabledIndices, initialSelection,
                    true, false, (materialDialog, indices, text) -> {
                        ToastUtils.showShort("Selected item  " + text + " at index " + indices.length);
                        return null;
                    });
            dialog.positiveButton(-1, "确定", materialDialog -> {
                ToastUtils.showShort("确定");
                return null;
            });
            dialog.show();
        });

        binding.btnButtons.setOnClickListener(view -> {
            MaterialDialog dialog = new MaterialDialog(DialogActivity.this, MaterialDialog.getDEFAULT_BEHAVIOR());
            dialog.setTitle("标题");
            dialog.message(-1, "消息", null);
            dialog.positiveButton(null, "Hello World", materialDialog -> {
                ToastUtils.showShort("Hello World");
                return null;
            });
            dialog.negativeButton(null, "How are you doing?", materialDialog -> {
                ToastUtils.showShort("How are you doing?");
                return null;
            });
            dialog.neutralButton(null, "Testing long buttons", materialDialog -> {
                ToastUtils.showShort("Testing long buttons");
                return null;
            });
            DialogCheckboxExtKt.checkBoxPrompt(dialog, -1, "选择", false, isChecked -> {
                ToastUtils.showShort("checked? " + isChecked);
                return null;
            });
            dialog.show();
        });

        binding.btnInputCheck.setOnClickListener(view -> {
            MaterialDialog dialog = new MaterialDialog(DialogActivity.this, MaterialDialog.getDEFAULT_BEHAVIOR());
            dialog.setTitle("标题");
            dialog.message(-1, "消息", null);
            DialogInputExtKt.input(dialog, "Type something", null, null, null,
                    InputType.TYPE_CLASS_TEXT,
                    null, true, false, (materialDialog, text) -> {
                        ToastUtils.showShort("Input  " + text);
                        return null;
                    });
            dialog.positiveButton(-1, "agree", materialDialog -> {
                ToastUtils.showShort("agree? ");
                return null;
            });
            dialog.negativeButton(-1, "disagree", materialDialog -> {
                ToastUtils.showShort("disagree");
                return null;
            });
            DialogCheckboxExtKt.checkBoxPrompt(dialog, -1, "checkboxConfirm", false, isChecked -> {
                ToastUtils.showShort("checked? " + isChecked);
                return null;
            });
            dialog.show();
        });

        binding.btnWebView.setOnClickListener(v -> {
            MaterialDialog dialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
            DialogCustomViewExtKt.customView(dialog, R.layout.dialog_view_webview, null,
                    false, true, false, false);
            dialog.setOnShowListener(onShowListener -> {
                WebView webView = DialogCustomViewExtKt.getCustomView(dialog).findViewById(R.id.web_view);
                webView.loadData(
                        "<h3>WebView Custom View</h3>\n" +
                                "\n" +
                                "<ol>\n" +
                                "    <li><b>NEW:</b> Hey!</li>\n" +
                                "    <li><b>IMPROVE:</b> Hello!</li>\n" +
                                "    <li><b>FIX:</b> Hi!</li>\n" +
                                "    <li><b>FIX:</b> Hey again!</li>\n" +
                                "    <li><b>FIX:</b> What?</li>\n" +
                                "    <li><b>FIX:</b> This is an example.</li>\n" +
                                "    <li><b>MISC:</b> How are you?</li>\n" +
                                "</ol>\n" +
                                "<p>Material guidelines for dialogs:\n" +
                                "    <a href='http://www.google.com/design/spec/components/dialogs.html'>" +
                                "http://www.google.com/design/spec/components/dialogs.html</a>.\n" +
                                "</p>",
                        "text/html",
                        "UTF-8");
            });
            dialog.show();
        });

        binding.btnColor.setOnClickListener(v -> {
            MaterialDialog dialog = new MaterialDialog(DialogActivity.this, MaterialDialog.getDEFAULT_BEHAVIOR());
            dialog.setTitle("标题");
            DialogColorChooserExtKt.colorChooser(dialog, ColorPalette.INSTANCE.getPrimary(), ColorPalette.INSTANCE.getPrimarySub(),
                    null, true, true, false, false,
                    (materialDialog, color) -> {
                        Toast.makeText(this, "Selected color: " + Integer.toHexString(color), Toast.LENGTH_SHORT).show();
                        return null;
                    });
            dialog.positiveButton(-1, "agree", materialDialog -> {
                ToastUtils.showShort("agree? ");
                return null;
            });
            dialog.negativeButton(android.R.string.cancel, null, materialDialog -> {
                ToastUtils.showShort("cancel");
                return null;
            });
            dialog.show();
        });

        binding.btnFile.setOnClickListener(v -> {
            MaterialDialog dialog = new MaterialDialog(DialogActivity.this, MaterialDialog.getDEFAULT_BEHAVIOR());
            dialog.setTitle("标题");
            DialogFileChooserExtKt.fileChooser(dialog, DialogActivity.this, getExternalFilesDir(null), file -> {
                        return (file.getName().endsWith("txt"));
                    },
                    true, R.string.files_default_empty_text, true, null,
                    (materialDialog, file) -> {
                        ToastUtils.showShort("Selected file: " + file.getAbsolutePath());
                        return null;
                    });
            dialog.positiveButton(-1, "select", materialDialog -> {
                ToastUtils.showShort("select");
                return null;
            });
            dialog.negativeButton(android.R.string.cancel, null, materialDialog -> {
                ToastUtils.showShort("cancel");
                return null;
            });
            dialog.show();
        });

        binding.btnBottomsheet.setOnClickListener(v -> {
            BottomSheet bottomSheet = new BottomSheet(LayoutMode.WRAP_CONTENT);
            MaterialDialog dialog = new MaterialDialog(DialogActivity.this, bottomSheet);
            dialog.title(-1, "useGoogleLocationServices");
            dialog.message(-1, "useGoogleLocationServicesPrompt", null);
            dialog.positiveButton(-1, "agree", materialDialog -> {
                ToastUtils.showShort("agree");
                return null;
            });
            dialog.negativeButton(-1, "disagree", materialDialog -> {
                ToastUtils.showShort("disagree");
                return null;
            });
            dialog.show();
        });

    }

}
